package com.hspedu.newData.dmsxl.动态规划.跳跃游戏;
/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-09 
 * @Version: 1.0
 */

public class LC134加油站 {

    // todo
    public int canCompleteCircuit(int[] gas, int[] cost) {
        // 对每一个加油站进行检查
        int n = gas.length;
        int i = 0;

        while (i<n){

            int sumGas = 0;
            int sumCost = 0;

            // 从每个加油站检查走的最远的距离
            int cnt = 0;

            while (cnt < n){
                int j = (i + cnt)%n;
                sumGas += gas[j];
                sumCost += cost[j];
                if (sumGas < sumCost){
                    break;
                }
                cnt++;
            }

            if (cnt == n){
                return i;
            }else {
                i = i + cnt + 1;
            }
        }

        // 跳出循环说明失败了
        return -1;
    }


}
